<script setup lang="ts" name="SystemNotifyRecord">
import { storeToRefs } from 'pinia'
import { useAppStoreWithOut } from '@/store/modules/app'
import { getIntDictOptions } from '@/utils/dict'
import { DICT_TYPE } from '@/enums/dictEnums'
import { dateTimeFormatter } from '@/utils/dateUtil'
import { NotifyRecordApi, NotifyRecordType } from '@/api/system/messages/notify/record'
import NotifyRecordDetail from './NotifyRecordDetail.vue'
const appStore = useAppStoreWithOut()
const { systemConfig } = storeToRefs(appStore)

const { t } = useI18n() // 国际化
const total = ref(0) // 列表的总页数
const list = ref([]) // 表格数据
const queryLoading = ref(false) // 列表加载中：1）列表数据加载；2）查询按钮禁用；3）重置按钮禁用
const queryParams = ref({
  pageIndex: 1,
  pageSize: 20,
  userType: undefined,
  userId: undefined,
  templateCode: undefined,
  templateType: undefined,
  create_time: []
})
const queryFormRef = ref() // 搜索的表单


/** 查询列表 */
const getList = async () => {
  queryLoading.value = true
  try {
    const data = await NotifyRecordApi.page(queryParams.value)
    list.value = data.list
    total.value = data.total
  } finally {
    queryLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageIndex = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.value.pageIndex = 1
  queryParams.value.pageSize = 20
  queryParams.value.userType = undefined
  queryParams.value.userId = undefined
  queryParams.value.templateCode = undefined
  queryParams.value.templateType = undefined
  queryParams.value.create_time = []
  handleQuery()
}


/** 详情操作 */
const detailRef = ref()
const openDetail = (data: NotifyRecordType) => {
  detailRef.value.open(data)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<template>
  <PageCard>
    <ContentFormTable>
      <template #header>
        <ElForm ref="queryFormRef" :model="queryParams" :inline="true">
          <ElFormItem :label="t('sys.messages.notify.record', 'userId')" prop="userId" class="!w-240px">
            <el-input v-model="queryParams.userId" :placeholder="t('sys.messages.notify.record.rules', 'userId')" clearable @keyup.enter="handleQuery" />
          </ElFormItem>
          <ElFormItem :label="t('sys.messages.notify.record', 'userType')" prop="userType">
            <el-select v-model="queryParams.userType" class="!w-160px" clearable :placeholder="t('sys.messages.notify.record.rules', 'userType')" @change="handleQuery">
              <el-option v-for="dict in getIntDictOptions(DICT_TYPE.USER_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </ElFormItem>
          <ElFormItem :label="t('sys.messages.notify.record', 'templateCode')" prop="templateCode" class="!w-240px">
            <el-input v-model="queryParams.templateCode" :placeholder="t('sys.messages.notify.record.rules', 'templateCode')" clearable @keyup.enter="handleQuery" />
          </ElFormItem>
          <ElFormItem :label="t('sys.messages.notify.record', 'templateType')" prop="templateType">
            <el-select v-model="queryParams.templateType" class="!w-160px" clearable :placeholder="t('sys.messages.notify.record.rules', 'templateType')" @change="handleQuery">
              <el-option v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE)" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </ElFormItem>
          <ElFormItem :label="t('common', 'createTime')" prop="create_time">
            <el-date-picker
              v-model="queryParams.create_time"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="!w-240px"
              clearable
              :start-placeholder="t('common', 'startDate')"
              :end-placeholder="t('common', 'endDate')"
              type="daterange"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="handleQuery" />
          </ElFormItem>
          <ElFormItem>
            <el-button type="primary" :disabled="queryLoading" icon="Search" @click="handleQuery">{{ t('action',
              'search') }}</el-button>
            <el-button icon="Refresh" :disabled="queryLoading" @click="resetQuery">{{ t('action', 'reset')
            }}</el-button>
          </ElFormItem>
        </ElForm>
      </template>
      <template #content>
        <el-table v-loading="queryLoading && systemConfig.loadingType === 'module'" :data="list" border>
          <el-table-column prop="id" :label="t('common', 'id')" width="60" align="center" />
          <el-table-column align="center" :label="t('sys.messages.notify.record', 'userType')" prop="userType">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.USER_TYPE" :value="scope.row.userType" />
            </template>
          </el-table-column>
          <el-table-column :label="t('sys.messages.notify.record', 'userId')" align="center" prop="userId" />
          <el-table-column :label="t('sys.messages.notify.record', 'templateCode')" align="center" prop="templateCode" />
          <el-table-column :label="t('sys.messages.notify.record', 'templateNickname')" align="center" prop="templateNickname" />
          <el-table-column :label="t('sys.messages.notify.record', 'templateContent')" align="center" show-overflow-tooltip prop="templateContent" />
          <el-table-column :label="t('sys.messages.notify.record', 'templateParams')" align="center" prop="templateParams" width="180" show-overflow-tooltip>
            <template #default="scope"> {{ scope.row.templateParams }}</template>
          </el-table-column>
          <el-table-column :label="t('sys.messages.notify.record', 'templateType')" align="center" prop="templateType" width="120">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE" :value="scope.row.templateType" />
            </template>
          </el-table-column>
          <el-table-column :label="t('sys.messages.notify.record', 'readStatus')" align="center" prop="readStatus" width="100">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.readStatus" />
            </template>
          </el-table-column>
          <el-table-column :label="t('sys.messages.notify.record', 'readTime')" align="center" prop="readTime" width="180" :formatter="dateTimeFormatter" />
          <el-table-column prop="create_time" :label="t('common', 'createTime')" width="180" :formatter="dateTimeFormatter" align="center" />
          <el-table-column prop="operate" :label="t('common', 'operate')" width="90" align="center" fixed="right">
            <template #default="scope">
              <el-button type="primary" @click="openDetail(scope.row)">{{ t('action', 'detail') }}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template #footer>
        <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageIndex" :total="total" :disabled="queryLoading" @pagination="getList" />
        <NotifyRecordDetail ref="detailRef" />
      </template>
    </ContentFormTable>
  </PageCard>
</template>

<style scoped lang="scss">
.el-table {
  width: 100%;
  height: 100%;
}
</style>
